<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

class UtilsController extends Controller
{
    public function longUrl2ShortUrl(Request $request)
    {
        $url_long = $request->input('url_long');
        $url = "http://api.t.sina.com.cn/short_url/shorten.json?source=3271760578&url_long=" . urlencode($url_long);
        echo get($url);
    }

    public function getQRCode(Request $request)
    {
        $url = $request->input('url');
//        echo $url;exit;
        if ($url) {
//            $img = "http://qr.liantu.com/api.php?text=" . urlencode($url);
//            return file_get_contents($img);

            $img = "https://www.kuaizhan.com/common/encode-png?large=true&data=" . urlencode($url);

            header("location:$img");
            return;
//            return $this->response->array([
//                'url' => "http://bshare.optimix.asia/barCode?site=weixin&url=" . urlencode($url)
//            ]);
        } else {
            return "";
        }
    }

    /**
     * @param Request $request
     * @author zhengmanbin
     * 发送天气预报给企业微信
     */
    public function sendWeatherToEnterpriseWechat(Request $request)
    {
        $key = $request->input('key');
        $url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" . $key;
        $weather = file_get_contents('https://www.tianqiapi.com/api/?city='.urlencode("深圳"));
//        echo $weather;exit;
        $weather = json_decode($weather);

        if (isset($weather->data[0])) {
            $w = $weather->data[0];
            $body = $w->date . " " . $w->week . " " . $weather->city . " " . "<font color='warning'>$w->wea</font>" ." "."<font color='warning'>$w->tem2~$w->tem1</font>". "\n" . $w->air_tips;
            echo $body;
        }
        $ret = post($url, '{"msgtype": "markdown","markdown":{"content": "' . $body . '"}}');
        var_dump($ret);
    }


}
